﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Model;

namespace TrainingsApp.IPADApp.Master {
    public partial class ClassManager : PageBase
    {
     

        public int SchoolId {
            get {
                return ZJFramework10.Utility.QueryStringManager.GetValue<int>("sid");
            }
        }



        protected void Page_Load(object sender, EventArgs e) {

            if (SchoolId <= 0) { RedirectTo("SchoolManager.aspx"); return; }
            DataBinds();

        }


        protected void DataBinds() {
            var dataSource = TrainingsApp.Logic.TS_Class_Logic.TS_Class_ListBySchoolId(SchoolId);
            this.aspDataSource.DataSource = dataSource;
            this.aspDataSource.DataBind();
        }

        protected void aspDataSource_RowDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e) {
            e.ExceptionHandled = true;
        }

        protected void aspDataSource_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
            if (SchoolId > 0) {

                int key = Convert.ToInt32(e.Keys["Idx"]);

                TrainingsApp.Logic.TS_Class_Logic.TS_Class_Delete(key);

                DataBinds();
            }
        }

        protected void aspDataSource_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e) {
            e.ExceptionHandled = true;
        }

        protected void aspDataSource_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {
            if (SchoolId > 0) {
                int gradeValue = Convert.ToInt32(e.NewValues["Grade"]);
                string className = e.NewValues["ClassName"] == null ? string.Empty : e.NewValues["ClassName"].ToString();


                TS_Class_Entity classEntity = new TS_Class_Entity();
                classEntity.ClassName = className;
                classEntity.Grade = gradeValue;
                classEntity.SchoolId = SchoolId;
                TrainingsApp.Logic.TS_Class_Logic.TS_Class_Add(classEntity);

                DataBinds();

            }
        }

        protected void aspDataSource_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e) {
            e.ExceptionHandled = true;
        }

        protected void aspDataSource_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
            if (SchoolId > 0) {
                int key = Convert.ToInt32(e.Keys["Idx"]);
                int gradeValue = Convert.ToInt32(e.NewValues["Grade"]);
                string className = e.NewValues["ClassName"] == null ? string.Empty : e.NewValues["ClassName"].ToString();


                TS_Class_Entity classEntity = new TS_Class_Entity();
                classEntity.Idx = key;
                classEntity.ClassName = className;
                classEntity.Grade = gradeValue;
                classEntity.SchoolId = SchoolId;
                TrainingsApp.Logic.TS_Class_Logic.TS_Class_Update(classEntity);

                DataBinds();


            }
        }
    }
}